//
//  BCMonetaryUnitView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/16.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCMonetaryUnitView.h"

/**
 单行高度 根据source.count计算去总的高度
 */
static CGFloat BCMonetaryUnitViewSingleHeight = 40.0f;
//箭头的宽度
static CGFloat BCMonetaryUnitViewArrowWidth = 6.0;
//content 宽度
static CGFloat BCMonetaryUnitViewContentWidth = 66.0f;


@interface BCMonetaryUnitView ()

@property (nonatomic, strong) UIView *contentView;

/**
 *arrowLayer
 */
@property (nonatomic, strong) CAShapeLayer *arrowLayer;


/**
 *isLeft
 */
@property (nonatomic, assign) BOOL isLeft;

@end

@implementation BCMonetaryUnitView

- (instancetype)initWithArrowLeft:(BOOL)left
{
    self = [super init];
    if (self) {
        self.isLeft = left;
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 5;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.height.mas_equalTo(self);
        make.width.mas_equalTo(BCMonetaryUnitViewContentWidth);
        if (self.isLeft) {
            make.right.mas_equalTo(self);
        }else {
            make.left.mas_equalTo(self);
        }
    }];
    //
    self.contentView.layer.shadowOpacity = 1;
    self.contentView.layer.shadowRadius = 5;
    self.contentView.layer.shadowColor = RGBHEXA(0x000000, 0.14).CGColor;
    self.contentView.layer.shadowOffset = self.isLeft?CGSizeMake(1, 1):CGSizeMake(-1, 1);
    
    //
    self.arrowLayer = [CAShapeLayer layer];
    self.arrowLayer.fillColor = self.contentView.backgroundColor.CGColor;
    self.arrowLayer.strokeColor = self.arrowLayer.fillColor;
    [self.layer addSublayer:self.arrowLayer];
}

- (void)setArrowCenterY:(CGFloat)arrowCenterY maxWidth:(CGFloat)width {
    CGFloat otherLenght = BCMonetaryUnitViewArrowWidth+3;
    CGFloat endY = arrowCenterY-otherLenght/2;
    if (endY<self.contentView.layer.cornerRadius) {
        self.arrowLayer.path = nil;
        return;
    }
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    if (self.isLeft) {
        CGPoint startPoint = CGPointMake(0, arrowCenterY);
        [bPath moveToPoint:startPoint];
        [bPath addLineToPoint:CGPointMake(BCMonetaryUnitViewArrowWidth, endY)];
        [bPath addLineToPoint:CGPointMake(BCMonetaryUnitViewArrowWidth, endY+otherLenght)];
        [bPath closePath];
    }else {
        CGPoint startPoint = CGPointMake(width, arrowCenterY);
        [bPath moveToPoint:startPoint];
        [bPath addLineToPoint:CGPointMake(width-BCMonetaryUnitViewArrowWidth, endY)];
        [bPath addLineToPoint:CGPointMake(width-BCMonetaryUnitViewArrowWidth, endY+otherLenght)];
        [bPath closePath];
    }
    self.arrowLayer.path = bPath.CGPath;
}


- (void)setMonetaryUnitViewSource:(NSArray <NSString *> *)source {
    [self.contentView qmui_removeAllSubviews];
    NSInteger count = source.count;
    for (int i=0; i<count; i++) {
        NSString *monetaryUnit = source[i];
        QMUIButton *button = [QMUIButton new];
        [button setTitleColor:[UIColor qmui_colorWithHexString:@"#787878"] forState:UIControlStateNormal];
        [button setTitle:monetaryUnit forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [button addTarget:self action:@selector(monetaryUnitButtonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        button.frame = CGRectMake(0, i*BCMonetaryUnitViewSingleHeight, BCMonetaryUnitViewContentWidth, BCMonetaryUnitViewSingleHeight);
    }
    //分隔线
    count -= 1;
    CGFloat leftPadding = BCMonetaryUnitViewContentWidth*0.1;
    for (int i=0; i<count; i++) {
        UIView *sepLine = [UIView new];
        sepLine.backgroundColor = [UIColor separatorColor];
        [self.contentView addSubview:sepLine];
        sepLine.frame = CGRectMake(leftPadding, (i+1)*BCMonetaryUnitViewSingleHeight-0.5, BCMonetaryUnitViewContentWidth-2*leftPadding, 1);
    }
}

#pragma mark - monetaryUnitButtonDidClickedAction
- (void)monetaryUnitButtonDidClickedAction:(QMUIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(monetaryUnitViewDidSelectMonetaryUnit:)]) {
        NSString *monetaryUnit = [button titleForState:UIControlStateNormal];
        [self.delegate monetaryUnitViewDidSelectMonetaryUnit:monetaryUnit];
    }
}





/**
 提前计算出大小 根据需要显示source
 */
+ (CGSize)monetaryUnitSize:(NSArray <NSString *> *)source {
    NSInteger count = source.count;
    if (count) {
        CGFloat height = count*BCMonetaryUnitViewSingleHeight;
        CGFloat width = (BCMonetaryUnitViewContentWidth+BCMonetaryUnitViewArrowWidth);
        return CGSizeMake(width, height);
    }
    return CGSizeZero;
}

@end
